﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Yelper.Phone;
using Yelper.API;

namespace Yelper_Phone
{
    public partial class NewYelp : PhoneApplicationPage
    {
        public NewYelp()
        {
            InitializeComponent();            
            LayoutRoot.DataContext = YelperStatic.App;            
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
            YelperStatic.App.AddYelpCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(App_AddYelpCompleted);
            base.OnNavigatedTo(e);            
        }

        private void App_AddYelpCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) {
            Dispatcher.BeginInvoke(new Action(() => {
                YelperStatic.App.AddYelpCompleted -= new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(App_AddYelpCompleted);
                NavigationService.GoBack(); 
            }));
        }

        private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
        	TextBoxYelp.Focus();
            TextBoxYelp.CaretBrush = new SolidColorBrush((Resources["PhoneForegroundBrush"] as SolidColorBrush).Color); //BUG in phone
        }
        
        private void ApplicationBarIconButton_Click(object sender, System.EventArgs e)
        {
            YelperStatic.App.AddYelpAsync(new Yelp { Message = TextBoxYelp.Text.Replace('#', 'λ') });
        }        
    }
}
